home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / 3Dmodeling / pwlin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.4 KB  |  139 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* Tom Davis -- 1992 */
  19.  
  20. #include "3d.h"
  21. #include <stdio.h>
  22. #include <math.h>
  23. #define PI    3.1415926535
  24.  
  25. pwlin_t *newpwlin()
  26. {
  27.     pwlin_t *c = (pwlin_t *)malloc(sizeof(pwlin_t));
  28.     c->n = 0;
  29.     c->verts = 0;
  30.     return c;
  31. }
  32.  
  33. pwlin_t *makepwlin(long n, float *data)
  34. {
  35.     long    i;
  36.     pwlin_t    *c = newpwlin();
  37.  
  38.     c->n = n;
  39.     c->verts = (float *)malloc(n*3*sizeof(float));
  40.     for (i = 0; i < n*3; i++)
  41.     c->verts[i] = *data++;
  42.     return c;
  43. }
  44.  
  45. void freepwlin(pwlin_t *p)
  46. {
  47.     if (p->verts) free(p->verts);
  48.     free(p);
  49. }
  50.  
  51. pwlin_t *appendpwlins(pwlin_t *p1, pwlin_t *p2)
  52. {
  53.     pwlin_t    *p = newpwlin();
  54.     long    i, dup = 0;
  55.     float    *f;
  56.  
  57.     if (p1->verts[3*p1->n-3] == p2->verts[0] &&
  58.         p1->verts[3*p1->n-2] == p2->verts[1] &&
  59.         p1->verts[3*p1->n-1] == p2->verts[2]) dup = 1;
  60.     p->n = p1->n + p2->n - dup;
  61.     p->verts = f = (float *)malloc(p->n*3*sizeof(float));
  62.     for (i = 0; i < 3*p1->n; i++)
  63.     *f++ = p1->verts[i];
  64.     for (i = dup ? 3 : 0; i < 3*p2->n; i++)
  65.     *f++ = p2->verts[i];
  66.     return p;
  67. }
  68.  
  69. /* solidofrevolution rotates about the y-axis */
  70.  
  71. void smoothsave(long n, float n0[3], float p0[3],
  72.         float n1[3], float p1[3], float n2[3],
  73.         float p2[3], float n3[3], float p3[3])
  74. {
  75.     addquad(p0, p1, p2, p3, n0);
  76. }
  77.  
  78. /* The following code rotates a piecewise linear curve about
  79.  * the y-axis and makes a series of little quadrilaterals.
  80.  */
  81.  
  82. void solidofrevolution(pwlin_t *pwc, long n, void (*savefunc)())
  83. {
  84.     long    i, j;
  85.     float    theta, theta1;
  86.     float    c, s, c1, s1;
  87.     float    x, y, z = 0.0, x1, y1, z1 = 0.0;
  88.     float    p0[3], p1[3], p2[3], p3[3];
  89.     float    n0[3], n1[3], n2[3], n3[3];
  90.  
  91.     for (i = 0; i < pwc->n-1; i++)
  92.     for (j = 0; j < n; j++) {
  93.         theta = j*2.0*PI/n;
  94.         theta1 = (j < n-1) ? (j+1)*2.0*PI/n : 0.0;
  95.         c = cos(theta); s = sin(theta);
  96.         c1 = cos(theta1); s1 = sin(theta1);
  97.         x = pwc->verts[3*i]; y = pwc->verts[3*i+1];
  98.         z = pwc->verts[3*i+2]; x1 = pwc->verts[3*i+3];
  99.         y1 = pwc->verts[3*i+4]; z1 = pwc->verts[3*i+5];
  100.  
  101.         p3[0] = x*c - z*s;
  102.         p3[1] = y;
  103.         p3[2] = x*s + z*c;
  104.  
  105.         p2[0] = x1*c - z1*s;
  106.         p2[1] = y1;
  107.         p2[2] = x1*s + z1*c;
  108.  
  109.         p1[0] = x1*c1 - z1*s1;
  110.         p1[1] = y1;
  111.         p1[2] = x1*s1 + z1*c1;
  112.  
  113.         p0[0] = x*c1 - z*s1;
  114.         p0[1] = y;
  115.         p0[2] = x*s1 + z*c1;
  116.  
  117.         if (samepoint(p0, p1) || samepoint(p1, p2))
  118.         perpnorm(p0, p2, p3, n0);
  119.         else
  120.         perpnorm(p0, p1, p2, n0);
  121.  
  122.         m_xformpt(p0, p0, n0, n1);
  123.         m_xformptonly(p1, p1);
  124.         m_xformptonly(p2, p2);
  125.         m_xformptonly(p3, p3);
  126.  
  127.         (*savefunc)(ADD_QUAD, n1, p3, n1, p2, n1, p1, n1, p0);
  128.         
  129.     }
  130. }
  131.  
  132. void smoothsolidofrevolution(pwlin_t *pwc, long n, void (*savefunc)())
  133. {
  134.     solidofrevolution(pwc, n, smoothsave);
  135.     doverts();
  136.     doquads(savefunc);
  137. }
  138.  
  139.